﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PitOfDespair.Models.Character
{
	public class ModifiedStat : BaseStat
	{
		int _modValue; //amount added to base after mods

		readonly IList<ModifyingAttribute> _mods = new List<ModifyingAttribute>(); // which attributes modify this stat

		public ModifiedStat()
		{
			_modValue = 0;
		}

		public void AddModifier(ModifyingAttribute mod)
		{
			if (_mods.Any(m => m.Attribute.Name == mod.Attribute.Name))
				throw new InvalidOperationException("duplicate mod added:" + mod.Attribute.Name);
			_mods.Add(mod);
		}


		public new int AdjustedBaseValue
		{
			get { return BaseValue + BuffValue + _modValue; }
		}
		public void Update()
		{
			CalculateModValue();
		}

		public string GetModifyingAttributesToSerialize()
		{
			string temp = string.Empty;
			//foreach(var m in _mods)
			for (int cnt = 0; cnt < _mods.Count; cnt++)
			{
				var attrib = _mods[cnt].Attribute;
				temp += _mods[cnt].Attribute.Name;
				temp += "_" + _mods[cnt].Ratio;

				if (cnt < _mods.Count - 1)
					temp += "|";


			}
			
			return temp;
		}

		void CalculateModValue()
		{
			_modValue = 0;
			if (_mods.Any() == false)
				return;
			foreach (var m in _mods)
			{
				_modValue += (int)(m.Attribute.AdjustedBaseValue * m.Ratio);
			}
		}

	}
}